#include "define.h"

/*
void irq_gpio14()
{
	
	int *D0_add = 0xB0000000;	//D0's base address
	int *D1_add = 0xB0000010;	//D1's base address	
	int *E2_add = 0xB1000000;	//E2's base address
	int *GPIO_add = 0x80000500;	//GPIO datain address
	int *UserID_add = 0xB1012288;	//User ID address
	
	int *flag = 0x40000000;	//Compensation mode flag
	int *addr = 0x40000004;	//Store the compensation address
	int *length = 0x40000008;	//Store the compensation data length
	int *checkout = 0x4000000C; //Store the compensation data checkout
	int *length_left = 0x40000010;	//Store the compensation data length left
	int *data_add_offset = 0x40000014;	//Store the offset of next data
	int *checkout_c = 0x40000018;	//check out
	int *data_add = 0x40000020;	//Store the compesation data here
	
	int i;
	int GPIO13;	//gpio[13]
	int comp_len;	//compensation length left
	int data1, data2;	//low and high 8 bit of a 16bit data
	int *comp_add;	//compensation address
	
	GPIO13 = ((*GPIO_add) >> 13) & 0x00000001;	//GPIO[13]
	
	if(*flag == 0x0 && GPIO13 == 1)	//Receive Header
	{
		if( (*(D0_add+3) == 0xA8A8) && (*(D1_add+3) == 0xA8A8) )
		{
			if( *(D1_add+1) == *UserID_add )	//userID match
			{
				*flag = 0x1;	//Start compensation mode
				*addr = *(D0_add+1)	;	//Store the compensation address
				*length = *(D0_add+2);	//Store the compensation data length
				*length_left = *(D0_add+2);	//Store the compensation data length left
				*checkout = *(D0_add);
				*data_add_offset = 0x0;	//initial address offset
			}
		}
	}
	else if(*flag == 0x1 && GPIO13 == 0)	//Receive compensation data
	{
		comp_len = (*length_left)/2;	//how many 16 bits data left
		comp_add = data_add + *(data_add_offset);
		if( comp_len <= 4 )
		{
			for(i=comp_len; i>0; i--)
			{
				*(comp_add+(comp_len-i)) = *(D0_add+3-(comp_len-i));	//Store the data
//				while( *(comp_add+(comp_len-i)) != *(D0_add+3-(comp_len-i)) );	//wait until data is stored
			}
			*flag = 2;	//All data has received
		}
		else if( comp_len > 4 && comp_len <= 8 )
		{
			for(i=4; i>0; i--)
			{
				*(comp_add+(4-i)) = *(D0_add-1+i);	//Store the data
//				while( *(comp_add+(4-i)) != *(D0_add-1+i) );	//wait until data is stored
			}
			comp_len -= 4;
			for(i=comp_len; i>0; i--)
			{
				*(comp_add+4+(comp_len-i)) = *(D1_add+3-(comp_len-i));	//Store the data
//				while( *(comp_add+4+(comp_len-i)) != *(D1_add+3-(comp_len-i)) );	//wait until data is stored
			}
			*flag = 2;	//All data has received
		}
		else if( comp_len > 8 )
		{
			for(i=4; i>0; i--)
			{
				*(comp_add+(4-i)) = *(D0_add-1+i);	//Store the data
//				while( *(comp_add+(4-i)) != *(D0_add-1+i) );	//wait until data is stored
			}
			for(i=4; i>0; i--)
			{
				*(comp_add+(8-i)) = *(D1_add-1+i);	//Store the data
//				while( *(comp_add+(8-i)) != *(D1_add-1+i) );	//wait until data is stored
			}
			*data_add_offset = (*data_add_offset) + 8;	//Next addr
			*length_left = (*length_left) - 16;	//data left
		}
	}
	if( *flag == 0x2 )	//All data has been received, compute the checkout & store data into E2
	{
		*checkout_c=0x0;
		for(i=0; i<((*length)/2); i++)
		{
			data1 = (*(data_add+i)) & 0x000000FF;	//low 8 bit
			data2 = ((*(data_add+i)) >> 8) & 0x000000FF;	//high 8 bit 
			*checkout_c = (*checkout_c)+data1+data2 ;	//compute checkout	
		}
		*checkout_c = (*checkout_c) & 0x000000FF;	//low 8 bit of checkout
		if ( *checkout_c == *checkout )	//check if the chechout is right
		{
			comp_add = E2_add + (*addr);
			for(i=0; i<((*length)/2); i++)
			{
				*(comp_add+i) = *(data_add+i);	//Store data into E2
				while( *(comp_add+i) != *(data_add+i) );	//wait until data is stored
			}
		}
		*flag = 0x0;
	}
}
*/

void irq_gpio()
{
	struct gpio_regs *gpio_r = (struct gpio_regs *) GPIO_ADD;
	
	if(((gpio_r->din) & 0x00000001)==0x0)
	{
		asm("ta 0");	
	}
	
	/*
	else
	{
		irq_gpio14();
	}
	*/
}
